package com.qf.servlet;

import com.qf.service.BookService;
import com.qf.service.impl.BookServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:day11_booksystem
 * author:liuXin
 * time:2022/9/27 16:20
 * description:
 */
@WebServlet("/deletebook")
public class DeleteBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端数据
        String id = request.getParameter("id");
        System.out.println("id = " + id);
        //非空验证
        if(StringUtils.isEmpty(id)){
           response.sendRedirect("booklist");
           return;//*****
        }
        //调用业务逻辑
        try {
            int bid = Integer.parseInt(id);
            BookService bookService=new BookServiceImpl();
            bookService.removeBook(bid);
            //转发重定向 删除成功重新查询图书
            response.sendRedirect("booklistpage");
        } catch (Exception e) {//****
            e.printStackTrace();//给程序员
            //转发重定向
            request.setAttribute("msg","删除失败"+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
